import React, {
	Component
} from 'react';
import {
	Tools,
	Button,
} from "weaver-mobile";
import Timer from './Timer';
const getLabel = Tools.getLabel;

export default class TimerButton extends Component {
	constructor(props) {
		super(props);
		this.state = {
			isCountDown: true, //初始化为倒计时状态
		}
	}

	componentWillReceiveProps(nextProps) {
		if (this.props.reCountDown !== nextProps.reCountDown) {
			this.setState({
				isCountDown: true,
			})
		}
	}

	getCount = (count) => {
		//倒计时是否结束
		const isCountDownFinish = (count === 0);

		//倒计时结束，但是状态值isCountDown为true时，将isCountDown置为false。
		if (isCountDownFinish && this.state.isCountDown) {
			this.setState({
				isCountDown: false,
			})
		}
	}

	render() {
		const {
			isCountDown
		} = this.state;

		const {
			onClick
		} = this.props;

		return (
			<div className='timer-button'>
				<Button 
					disabled={isCountDown}
					type='primary' 
					onClick={onClick} 
					icon={ isCountDown && <Timer getCount={this.getCount}/>}
					className='override-default-button'
				>
					{!isCountDown ? getLabel(507765,'重新发送') : ''}
				</Button>
			</div>
		);
	}
}